﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;
using Common;
namespace WinUI.Frm_Program
{
    public partial class Frm_CreateProgram : Form
    {
        List<ComboItem> listCustomer;
        List<ComboItem> listBrand;
        List<ComboItem> listCS;
        public Frm_CreateProgram()
        {
            InitializeComponent();

            Init();
        }


        private void Frm_CreateProgram_Load(object sender, EventArgs e)
        {

        }

        private void Init()
        {
            //Init customer list
            listCustomer = new List<ComboItem>();
            Customer.GetAllForCombo(ref listCustomer);
            cb_Customer.DataSource = listCustomer;
            cb_Customer.DisplayMember = "Text";
            cb_Customer.ValueMember = "ID";
            cb_Customer.SelectedIndex = 0;

            //Init brand list
            //listBrand = new List<ComboItem>();
            //Brand.GetAllForCombo(ref listBrand, listCustomer.FirstOrDefault().ID);
            //cb_Product.DataSource = listBrand;
            //cb_Product.DisplayMember = "Text";
            //cb_Product.ValueMember = "ID";
            //cb_Product.SelectedIndex = 0;

            //Init cs list
            listCS = new List<ComboItem>();
            Employee.GetEmployeeByJobForCombo(ref listCS, EmployeeJob.CS);
            ccb_CS.Properties.DataSource = listCS;
            ccb_CS.Properties.DisplayMember = "Text";
            ccb_CS.Properties.ValueMember = "Value";

            //Init ProjectCode
            tb_ProgramCode.Text = Business.Program.CreateCode();
        }
        private void bt_Reset_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_OK_Click(object sender, EventArgs e)
        {
            Business.MyProgram pr = new Business.MyProgram();
            pr.ListEmployeeCode = new List<string>();
            pr.ProgramCode = tb_ProgramCode.Text;
            pr.ProgramName = tb_ProgramName.Text;
            pr.BrandID = int.Parse(cb_Product.SelectedValue.ToString());
            pr.StartDate = dE_FromDate.DateTime;
            pr.EndDate = dE_ToDate.DateTime;
            var listEmployeeCode = ccb_CS.EditValue.ToString().Split(',');
            foreach (var item in listEmployeeCode)
            {
                pr.ListEmployeeCode.Add(item.Trim());
            }
            Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW errCode = Business.Program.AddNew(ref pr, Global._UserInfo);
            ShowMessageBox((int)errCode);
        }

        private void ShowMessageBox(int errCode)
        {
            switch (errCode)
            {
                case 1: // SUCCESSS
                    MessageBox.Show("Thành công!");
                    this.Close();
                    break;
                default:
                    MessageBox.Show("Lỗi hệ thống!");
                    break;
            }
        }

        private void cb_Product_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cb_Customer_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cb_Customer.SelectedIndex;
            ComboItem item = listCustomer[index];
            //Init brand list
            if (listBrand == null)
            {
                listBrand = new List<ComboItem>();
                Brand.GetAllForCombo(ref listBrand, item.ID);
            }
            else
            {
                cb_Product.DataSource = null;
                listBrand = new List<ComboItem>();
                Brand.GetAllForCombo(ref listBrand, item.ID);
            }
            cb_Product.DataSource = listBrand;
            cb_Product.DisplayMember = "Text";
            cb_Product.ValueMember = "ID";
            if (listBrand.Count > 0)
                cb_Product.SelectedIndex = 0;
        }
    }
}
